package com.example.bingobox.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.Task;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
    private final List<Task> tasks;
    private final int showType; // 0: 未完成, 1: 已完成, 2: 已过期, 3: 搜索
    private final OnTaskStatusChangeListener statusListener;
    private final TaskActionListener actionListener;

    // 定义常量
    public static final int SHOW_INCOMPLETE = 0;
    public static final int SHOW_COMPLETED = 1;
    public static final int SHOW_EXPIRED = 2;
    public static final int SHOW_SEARCH = 3;

    public interface OnTaskStatusChangeListener {
        void onTaskStatusChanged(Task task, boolean completed);
    }

    public interface TaskActionListener {
        void onEditTask(Task task);
        void onDeleteTask(Task task);
        void onFavoriteTask(Task task, boolean favorite);
    }

    public TaskAdapter(List<Task> tasks, int showType, 
            OnTaskStatusChangeListener statusListener, TaskActionListener actionListener) {
        this.tasks = tasks;
        this.showType = showType;
        this.statusListener = statusListener;
        this.actionListener = actionListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox;
        TextView nameText;
        TextView descriptionText;
        TextView deadlineText;
        TextView completeTimeText;
        TextView cityTypeText;
        ImageButton editButton;
        ImageButton deleteButton;
        ImageButton favoriteButton;

        ViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.checkbox_task);
            nameText = itemView.findViewById(R.id.text_task_name);
            descriptionText = itemView.findViewById(R.id.text_task_description);
            deadlineText = itemView.findViewById(R.id.text_task_deadline);
            completeTimeText = itemView.findViewById(R.id.text_complete_time);
            cityTypeText = itemView.findViewById(R.id.text_city_type);
            editButton = itemView.findViewById(R.id.btn_edit);
            deleteButton = itemView.findViewById(R.id.btn_delete);
            favoriteButton = itemView.findViewById(R.id.btn_favorite);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_task, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Task task = tasks.get(position);
        
        holder.checkBox.setChecked(task.isCompleted());
        holder.nameText.setText(task.getName());
        holder.descriptionText.setText(task.getDescription());
        
        // 设置截止日期
        if (task.getDeadline() == 0) {
            holder.deadlineText.setText("永久");
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
            holder.deadlineText.setText(sdf.format(new Date(task.getDeadline())));
        }

        // 设置城市类型
        holder.cityTypeText.setText(task.getCityTypeText());
        
        // 设置完成时间
        if (task.isCompleted() && task.getCompleteTime() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault());
            holder.completeTimeText.setText("完成于: " + sdf.format(new Date(task.getCompleteTime())));
            holder.completeTimeText.setVisibility(View.VISIBLE);
        } else {
            holder.completeTimeText.setVisibility(View.GONE);
        }

        // 根据showType设置按钮显示状态
        if (showType == SHOW_SEARCH || showType == SHOW_INCOMPLETE) {
            // 搜索页面和未完成页面显示所有按钮
            holder.editButton.setVisibility(View.VISIBLE);
            holder.deleteButton.setVisibility(View.VISIBLE);
            holder.favoriteButton.setVisibility(View.VISIBLE);
            // 设置收藏按钮图标
            holder.favoriteButton.setImageResource(
                task.isFavorite() ? R.drawable.ic_favorite : R.drawable.ic_favorite_border
            );
        } else {
            // 其他页面只显示编辑和删除按钮
            holder.editButton.setVisibility(View.VISIBLE);
            holder.deleteButton.setVisibility(View.VISIBLE);
            holder.favoriteButton.setVisibility(View.GONE);
        }

        // 设置按钮点击事件
        holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (statusListener != null) {
                statusListener.onTaskStatusChanged(task, isChecked);
            }
        });

        holder.editButton.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onEditTask(task);
            }
        });

        holder.deleteButton.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onDeleteTask(task);
            }
        });

        holder.favoriteButton.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onFavoriteTask(task, !task.isFavorite());
                // 立即更新收藏按钮图标
                holder.favoriteButton.setImageResource(
                    !task.isFavorite() ? R.drawable.ic_favorite : R.drawable.ic_favorite_border
                );
            }
        });

        // 设置文本颜色
        if (showType == SHOW_EXPIRED) {
            // 已过期任务显示红色
            holder.deadlineText.setTextColor(Color.RED);
            holder.nameText.setTextColor(Color.RED);
        } else {
            // 其他情况使用默认颜色
            holder.deadlineText.setTextColor(holder.itemView.getContext()
                .getColor(android.R.color.holo_green_dark));
            holder.nameText.setTextColor(holder.itemView.getContext()
                .getColor(android.R.color.black));
        }
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    public void setTasks(List<Task> newTasks) {
        this.tasks.clear();
        this.tasks.addAll(newTasks);
        notifyDataSetChanged();
    }
} 